package com.sky.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sky.constant.MessageConstant;
import com.sky.context.BaseContext;
import com.sky.dto.OrdersPageQueryDTO;
import com.sky.entity.OrderDetail;
import com.sky.entity.Orders;
import com.sky.entity.ShoppingCart;
import com.sky.exception.OrderBusinessException;
import com.sky.mapper.HistoryMapper;
import com.sky.mapper.OrderDetailsMapper;
import com.sky.mapper.ShoppingCartMapper;
import com.sky.result.PageResult;
import com.sky.service.HistoryService;
import com.sky.vo.OrderVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

@Service
public class HistoryServiceImpl implements HistoryService {
    @Autowired
    private HistoryMapper historyMapper;
    @Autowired
    private OrderDetailsMapper orderDetailsMapper;
    @Autowired
    private ShoppingCartMapper shoppingCartMapper;

    /*
     * 查询历史订单
     * */
    @Override
    public PageResult historylist(OrdersPageQueryDTO ordersPageQueryDTO) {
        PageHelper.startPage(ordersPageQueryDTO.getPage(),ordersPageQueryDTO.getPageSize());
        Page<OrderVO> pages = historyMapper.histlistss(ordersPageQueryDTO);
        long total = pages.getTotal();
        List<OrderVO> result = pages.getResult();
        for (OrderVO i : result) {
            List<OrderDetail> orderDetails = orderDetailsMapper.queryByid(i.getId());
            i.setOrderDetailList(orderDetails);
        }
        PageResult pageResult = new PageResult();
        pageResult.setTotal(total);
        pageResult.setRecords(result);
        return pageResult;
    }

    /**
     *
     * 查询订单详情
     * @param id
     * @return
     */
    @Override
    public OrderVO list(long id) {
       List<OrderDetail>  list = orderDetailsMapper.sellectByid(id);
      OrderVO orderVO =  historyMapper.selectByid(id);
      orderVO.setOrderDetailList(list);
        return orderVO;
    }

    /**
     * 根据id取消订单
     * @param id
     */
    @Override
    public void cancel(long id) {
      Orders  orders = historyMapper.getBYid(id);
      if (orders==null){
          throw new  OrderBusinessException(MessageConstant.ORDER_NOT_FOUND);
      }
        //订单状态 1待付款 2待接单 3已接单 4派送中 5已完成 6已取消
      if (orders.getStatus()>2){
          throw new OrderBusinessException(MessageConstant.ORDER_STATUS_ERROR);
      }
                Orders orderD = new Orders();
                   orderD.setId(orders.getId());
        // 订单处于待接单状态下取消，需要进行退款
        if (orders.getStatus().equals(Orders.TO_BE_CONFIRMED)){
            //TODO 需要调用退款进行退款
        }
        //更新订单状态
        orderD.setStatus(Orders.CANCELLED);
        orderD.setCancelReason("用户取消订单");
        orderD.setCancelTime(LocalDateTime.now());
        historyMapper.Updatecancel(orderD);
    }

    /**
     * 再来一单
     * @param id
     */
    @Override
    public void reprtitiong(long id) {
       List<OrderDetail> lise =  orderDetailsMapper.queryByid(id);
        ShoppingCart orderDetail = new ShoppingCart();
        for (OrderDetail or : lise) {
            orderDetail.setName(or.getName());
            orderDetail.setUserId(BaseContext.getCurrentId());
            orderDetail.setDishId(or.getDishId());
            orderDetail.setSetmealId(or.getSetmealId());
            orderDetail.setDishFlavor(or.getDishFlavor());
            orderDetail.setNumber(or.getNumber());
            orderDetail.setAmount(or.getAmount());
            orderDetail.setImage(or.getImage());
            orderDetail.setCreateTime(LocalDateTime.now());
            shoppingCartMapper.add(orderDetail);
        }

    }
}
